package com.common.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.log4j.Logger;

public class RadioTag extends BaseTag {
	private Logger log = Logger.getLogger(this.getClass());
	private String id;
	private String name;
	private String value;
	private String beanName;
	

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		 StringBuilder outStr = new StringBuilder();
		 if(id==null){
			 id=name;
		 }
		 Map<String,String> beanMap = (TreeMap<String,String>) getServletContext().getAttribute(beanName);
		
		 //TreeMap<String,String> tMap = (TreeMap<String,String>)beanMap;
		 Iterator keyiter = beanMap.keySet().iterator();
		 while(keyiter.hasNext()){
			 String key = (String) keyiter.next();
			 String text = (String) beanMap.get(key);
			 outStr.append("<input type='radio' name='"+name+"' value='"+key+"'");
			 if(key!=null&&key.equals(value)){
				 outStr.append(" checked='checked'");
			 }
			 outStr.append(" />");
			 outStr.append(text);
			 outStr.append("<br />");
		 }
		 	
		//log.info("outStr====="+outStr);
		try {
			out.print(outStr);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return super.doStartTag();
	}


	public String getBeanName() {
		return beanName;
	}


	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getValue() {
		return value;
	}


	public void setValue(String value) {
		this.value = value;
	}


}
